//
//  ZaiXianDiaoChaViewController.m
//  tangshantongjiAPPFull
//
//  Created by yuntai on 2018/4/23.
//  Copyright © 2018年 yuntai. All rights reserved.
//

#import "ZaiXianDiaoChaViewController.h"
#import "FullMainModel.h"
#import "NewsDetailViewController.h"

@interface ZaiXianDiaoChaViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic ,strong)NSMutableArray *array;
@property (nonatomic, strong) UITableView *tableview;

@end

@implementation ZaiXianDiaoChaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.array = [NSMutableArray array];

    UITableView *tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, kNavigationHeight, kScreenBoundWidth, kScreenBoundHeight-kNavigationHeight) style:UITableViewStylePlain];
    tableview.delegate = self;
    tableview.dataSource = self;
    tableview.rowHeight = 66;
    tableview.sectionHeaderHeight = 30;
    tableview.tableFooterView = [[UIView alloc]init];
    [tableview setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.tableview = tableview;
    [self.view addSubview:tableview];
    BOOL ishow = self.array.count ? NO : YES;
    [self.tableview showNoView:nil image:nil certer:CGPointZero isShow:ishow];
    [self.tableview reloadData];
    
    weakself(self)
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakself.currentPage = 1;
        [weakself getdata];
    }];
    [self.tableview.mj_header beginRefreshing];
    
    self.tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakself getdata];
    }];
    
}

#pragma mark tableviewdelegate datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.array.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *pricecellid = @"pricevideocellid";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:pricecellid];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:pricecellid];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        UIImageView *yuanview = [[UIImageView alloc]initWithFrame:CGRectMake(15, 10, 46, 46)];
        yuanview.layer.cornerRadius = 4;
        yuanview.layer.masksToBounds = YES;
        yuanview.tag = 100;
        [yuanview setContentMode:UIViewContentModeScaleAspectFill];
        yuanview.image = [UIImage imageNamed:@"logo"];
        [cell.contentView addSubview:yuanview];
        
        UILabel *namel = [[UILabel alloc]initWithFrame:CGRectMake(70, 0, kScreenBoundWidth-80, 33)];
        namel.tag = 101;
        namel.textColor = [UIColor blackColor];
        namel.font = [UIFont boldSystemFontOfSize:16];
        [cell.contentView addSubview:namel];
        
        UILabel *namel2 = [[UILabel alloc]initWithFrame:CGRectMake(70, 33, kScreenBoundWidth-100, 22)];
        namel2.tag = 102;
        namel2.textColor = kTextDeepDarkColor;
        namel2.font = [UIFont systemFontOfSize:13];
        [cell.contentView addSubview:namel2];
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 66-0.7, kScreenBoundWidth, 0.7)];
        line.backgroundColor = KSepLineColor;
        [cell.contentView addSubview:line];
    }
    
    FullMainModel *model = self.array[indexPath.section];

    UILabel *nl = [cell.contentView viewWithTag:101];
    nl.text = model.name?:@"";

    UILabel *snl = [cell.contentView viewWithTag:102];
    if (self.type == XiaoJiaDiaoCha) {
        snl.text = model.memo?:@"";
    }else{
        snl.text = model.remark?:@"";
    }
    
    if(self.type == WenXinTishi){
        UIImageView *iv = [cell.contentView viewWithTag:100];
        [ iv sd_setImageWithURL:[NSURL URLWithString:model.img_path?:@""] placeholderImage:[UIImage imageNamed:@"logo"]];
    }
    return cell;

}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    FullMainModel *model = self.array[indexPath.section];
    NewsDetailViewController *vc = [[NewsDetailViewController alloc]init];
    if(self.type != WenXinTishi && self.type != XiaoJiaDiaoCha){
        NSString *urlstr = [NSString stringWithFormat:@"%@%@%@%@%@",SERVER_NEW_NETWORK_PRODUCT,@"api/testPaper/loadPaperDetailPage.htm?token=",kTokenID,@"&paperId=",model.modelId];
        vc.urlStr = urlstr?:@"";
        vc.isHidenNavi = YES;
    }else{
        vc.urlStr = model.path?:@"";
    }
    vc.title = model.name;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)getdata{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    

    NSString *subUrl = nil;
    if(self.type == WenXinTishi){
        subUrl = @"api/index/getFriendlyReminder.ajax";
        [params setValue:kTokenID forKey:@"token"];
        [params setValue:@"1" forKey:@"mid"];
        [params setObject:@"" forKey:@"title"];
        [params setValue:@(self.currentPage) forKey:@"page"];

    }else if (self.type == XiaoJiaDiaoCha){
        subUrl = @"api/index/getDateByMid.ajax";
        [params setValue:kTokenID forKey:@"token"];
        [params setValue:self.modelid forKey:@"mid"];
        [params setObject:@"0" forKey:@"type"];
        [params setValue:@(self.currentPage) forKey:@"page"];
        
        
    }else{
        subUrl = @"api/testPaper/all.ajax";
        [params setValue:kTokenID forKey:@"token"];
        [params setValue:kDefaultPageSize forKey:@"rows"];
        [params setValue:@(self.currentPage) forKey:@"page"];
        if(self.type == ZaiXianDiaoCha){
            [params setValue:@"1" forKey:@"type"];
        }else{
            [params setValue:@"2" forKey:@"type"];
        }
    }
    [NetWork PostNetWorkWithUrl:subUrl with:params successBlock:^(NSDictionary *dic) {
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
        
        NSArray *temparray = nil;
        if(self.type == WenXinTishi || self.type == XiaoJiaDiaoCha){
            temparray = dic[@"data"][@"data"];
        }else{
            temparray = dic[@"data"][@"lists"];
        }
        if (self.currentPage == 1) {
            [self.array removeAllObjects];
        }
        if (temparray.count<20) {
            [self.tableview.mj_footer endRefreshingWithNoMoreData];
        }else{
            self.currentPage ++;
            [self.tableview.mj_footer resetNoMoreData];
        }
        [self.array addObjectsFromArray:[FullMainModel mj_objectArrayWithKeyValuesArray:temparray]];
        BOOL ishow = self.array.count ? NO : YES;
        [self.tableview showNoView:nil image:nil certer:CGPointZero isShow:ishow];
        [self.tableview reloadData];
        
    } FailureBlock:^(NSString *msg) {
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
        [HUDManager showWarningWithText:msg];
        BOOL ishow = self.array.count ? NO : YES;
        [self.tableview showNoView:nil image:nil certer:CGPointZero isShow:ishow];
        
        
    } errorBlock:^(id error) {
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
        BOOL ishow = self.array.count ? NO : YES;
        [self.tableview showNoView:nil image:nil certer:CGPointZero isShow:ishow];
        
        
    }];
    
}


@end
